/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.controller;

import com.reddys.rad.inventory.db.Stock;
import com.reddys.rad.inventory.db.StockDetails;
import com.reddys.rad.inventory.service.StockManagementService;
import com.reddys.rad.inventory.service.StockMovementHistoryService;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;

/**
 * FXML Controller class
 *
 * @author REDDYS
 */
public class StockManagementController implements Initializable {

    private  ObservableList data =  FXCollections.observableArrayList();

    @FXML
    TableView tableView;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        populateTableView();
    }    

    private void populateTableView() {
       List list = getStockManagementService().getStockDetails();
       for(int i=0;i<list.size();i++){
           Stock stock = (Stock) list.get(i);
           ObservableList dataRow = FXCollections.observableArrayList();
           dataRow.add(stock.getProductCategory());
           dataRow.add(stock.getProductId());
           dataRow.add(stock.getProductDescription());
           dataRow.add(stock.getAvailableQuantity()!=null?stock.getAvailableQuantity().toString():"");
           data.add(dataRow);
       }
       createTableColumns();
       tableView.getItems().addAll(data);
    }
    
    private  StockManagementService getStockManagementService(){
        StockManagementService stockManagementService = new StockManagementService();
        return stockManagementService;
    }

    private void createTableColumns() {
          TableColumn productCategory = new TableColumn("Product Category");
          productCategory.setMinWidth(300);
          createCellValueFactory(productCategory,0);
          TableColumn productID = new TableColumn("Product ID");
          productID.setMinWidth(350);
          createCellValueFactory(productID,1);
          TableColumn productName = new TableColumn("Product Name");
          productName.setMinWidth(350);
          createCellValueFactory(productName,2);
          TableColumn availableQuantity = new TableColumn("Available Quantity");
          availableQuantity.setMinWidth(350);
          createCellValueFactory(availableQuantity,3);
          tableView.getColumns().addAll(productCategory,productID,productName,availableQuantity);
    }
    private void createCellValueFactory(TableColumn tableColumn,int columnNo){
        final int columnIndex = columnNo;     
        tableColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
             
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
              return new SimpleStringProperty( 
                      cdf.getValue().get(columnIndex)!=null?cdf.getValue().get(columnIndex).toString():"");
            }
              });

    }

      
}
